﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using btnet;
using System.Data;

/// <summary>
/// Summary description for delete_bug
/// </summary>
public partial class delete_bug : System.Web.UI.Page
{
    protected String sql;

    protected Security security;

    void Page_Init(object sender, EventArgs e) { ViewStateUserKey = Session.SessionID; }

    ///////////////////////////////////////////////////////////////////////
    void Page_Load(Object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            String footer_label = null;
            if (Application["custom_footer"] != null)
            {
                footer_label = (String)Application["custom_footer"];
            }
            custom_footer_label.Text = footer_label;
        }

        Util.do_not_cache(Response);

        security = new Security();
        security.check_security(HttpContext.Current, Security.ANY_USER_OK_EXCEPT_GUEST);

        if (security.user.is_admin || security.user.can_delete_bug)
        {
            //
        }
        else
        {
            Response.Write("You are not allowed to use this page.");
            Response.End();
        }

        string id = Util.sanitize_integer(Request["id"]);

        int permission_level = btnet.Bug.get_bug_permission_level(Convert.ToInt32(id), security);
        if (permission_level != Security.PERMISSION_ALL)
        {
            Response.Write("You are not allowed to edit this item");
            Response.End();
        }

        if (IsPostBack)
        {
            Bug.delete_bug(Convert.ToInt32(row_id.Value));
            Server.Transfer("bugs.aspx");
        }
        else
        {
            titl.InnerText = Util.get_setting("AppTitle", "BugTracker.NET") + " - "
                + "delete " + Util.get_setting("SingularBugLabel", "bug");

            back_href.HRef = "edit_bug.aspx?id=" + id;

            sql = @"select bg_short_desc from bugs where bg_id = $1";
            sql = sql.Replace("$1", id);

            DataRow dr = btnet.DbUtil.get_datarow(sql);

            confirm_href.InnerText = "confirm delete of "
                    + Util.get_setting("SingularBugLabel", "bug")
                    + ": "
                    + Convert.ToString(dr["bg_short_desc"]);

            row_id.Value = id;
        }

    }
}